package com.team7.happycommunity.personcentor.controller;

import com.team7.happycommunity.personcentor.common.CommonResult;
import com.team7.happycommunity.personcentor.entity.CommunityDynamic;
import com.team7.happycommunity.personcentor.entity.CommunityDynamicComment;
import com.team7.happycommunity.personcentor.entity.DynamicCommit;
import com.team7.happycommunity.personcentor.service.CommunityDynamicCommentService;
import com.team7.happycommunity.personcentor.service.CommunityDynamicService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (CommunityDynamic)表控制层
 *
 * @author makejava
 * @since 2020-03-24 17:26:32
 */
@RestController
@RequestMapping("communityDynamic")
@Api(tags="社区动态接口")
public class CommunityDynamicController {
    /**
     * 服务对象
     */
    @Resource
    private CommunityDynamicService communityDynamicService;
    @Resource
    private CommunityDynamicCommentService communityDynamicCommentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public CommunityDynamic selectOne(Integer id) {
        return this.communityDynamicService.queryById(id);
    }

    /**
     * 查询当前动态的所有评论
     * @param dynamicId 动态id
     * @return 评论内容
     */
    @GetMapping("queryCommit")
    @ApiOperation(value = "查询当前动态的所有评论")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "dynamicId",value = "动态id",required = true)
    })
    public List<DynamicCommit> queryCommit(Integer dynamicId){
      return   communityDynamicService.queryCommit(dynamicId);
    }

    /**
     * 添加评论
     * @param dynamicId 动态id
     * @param userId 评论用户的id
     * @param cdcContent 评论内容
     * @return
     */
    @GetMapping("addCommit")
    @ApiOperation(value = "添加评论")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "dynamicId",value = "动态id",required = true),
            @ApiImplicitParam(name = "userId",value = "评论用户的id",required = true),
            @ApiImplicitParam(name = "userId",value = "评论内容",required = true)
    })

    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "评论成功"),
            @ApiResponse(code = 500,message = "评论失败")
    })
    public CommonResult addCommit(Integer dynamicId,Integer userId,String cdcContent){
        try {
            CommunityDynamicComment comment=new CommunityDynamicComment();
            comment.setCdcContent(cdcContent);
            comment.setCdcDynamicId(dynamicId);
            comment.setCdcUserId(userId);
            comment.setCdcTime(new Date());
            communityDynamicCommentService.insert(comment);
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    /**
     * 删除评论
     * @param id 评论的id
     * @return
     */
    @GetMapping("deleteCommit")
    @ApiOperation(value = "删除评论")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "评论id",required = true)
    })

    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    public CommonResult deleteCommit(Integer id){
        try {
            communityDynamicCommentService.deleteById(id);
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
    }
}